package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.User;
import com.blb.entity.Ware;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.IWareService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import com.blb.entity.CargoArea;
import com.blb.service.ICargoAreaService;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-16
 */
@Controller
@RequestMapping("/cargoarea")
public class CargoAreaController {
    @Resource
    private ICargoAreaService iCargoAreaService;

    @Resource
    private IWareService wareService;

    @RequestMapping("{page}")
    public String wel(@PathVariable String page){
        return "warehouse/"+ page;
    }


    /* 查询分页列表和模糊查询 */
    @ResponseBody
    @RequestMapping("cargo")
    public BlbPage cargo(HttpSession session,String wareId, String goodsName, String deteTime , int start, int length){
        System.out.println(wareId);

        /*查询分页数据*/
        List<Map<String,Object>>  allCargoArea = iCargoAreaService.findAllCargoArea(start, length,wareId, goodsName, deteTime , ShiroUtils.getUser().getCompanyId());
        System.out.println(allCargoArea);

        int count = iCargoAreaService.count(wareId,goodsName, deteTime);

        return new DataTablePage(count, count, allCargoArea);

    }

    @ResponseBody
    @RequestMapping("findAllCargo")
    public ResponseHelp findAllWare2(@RequestBody CargoArea cargoArea){
        List<CargoArea> allcarByWareId = iCargoAreaService.findAllCarByWareId(cargoArea);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(allcarByWareId);
        return responseHelp;
    }

    /* 添加仓库前判断是否已经满了 */
    @ResponseBody
    @RequestMapping("cargoNum")
    public ResponseHelp findCarGoNum(String wareId){
        int cargoNum1 = Integer.parseInt(wareService.findCargoNumById(wareId));
        int cargoNum2 = iCargoAreaService.countCargoNum(wareId);
        List<Integer> list = new ArrayList<Integer>();
        list.add(cargoNum1);
        list.add(cargoNum2);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(list);
        return responseHelp;
    }

    /* 添加仓库 */
    @RequestMapping("addcargoarea")
    public String AddWare(@RequestBody CargoArea data, HttpSession session){
        System.out.println(data);
        /* 生成货区id */
        data.setId(UUIDUtils.getId());
        /* 插入公司id */
        data.setCompanyId(ShiroUtils.getUser().getCompanyId());
        data.setPersonId(data.getWareId());
        String status = "0";
        data.setStatus(status);
        iCargoAreaService.AddCargoArea(data);
        return "warehouse/cargoArea_manage";
    }

    /* 修改仓库 */
    @RequestMapping("updatecargo")
    public String UpdateWare(@RequestBody CargoArea data){
        iCargoAreaService.UpdateCargo(data);
        return "warehouse/cargoArea_manage";
    }

    /* 修改库区状态 */
    /* 修改库区启用状态 */
    @RequestMapping("updatecargostatus0")
    public String UpdateWareStatus(@RequestBody CargoArea data){
        iCargoAreaService.UpdateWareStatus(data);
        return "warehouse/cargoArea_manage";
    }

    /* 修改库区停用状态 */
    @RequestMapping("updatecargostatus1")
    public String UpdateWareStatus1(@RequestBody CargoArea data){
        iCargoAreaService.UpdateWareStatus1(data);
        return "warehouse/cargoArea_manage";
    }
}

